<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "basemessages".
 *
 * @property int $msgId 标识
 * @property int $typeId 消息类别
 * @property string $content 消息内容
 * @property bool $msgStatus 读取状态
 * @property string $msgDate 消息时间
 */
class Basemessages extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'basemessages';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['typeId'], 'integer'],
            [['content'], 'required'],
            [['content'], 'string'],
            [['msgStatus'], 'boolean'],
            [['msgDate'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'msgId' => 'Msg ID',
            'typeId' => 'Type ID',
            'content' => 'Content',
            'msgStatus' => 'Msg Status',
            'msgDate' => 'Msg Date',
        ];
    }

    public function getMessageData($post, $page, $pageSize)
    {
        $where = 1;
        $offset = max(0, ($page-1))*$pageSize;
        if (isset($post['s']) && !empty($post['s'])){
            $s = $post['s'].' 00:00:00';
            $where .= " and msgDate >= '{$s}' ";
        }
        if (isset($post['f']) && !empty($post['f'])){
            $f = $post['f'].' 23:59:59';
            $where .= " and msgDate <= '{$f}' ";
        }
        $sql = "select *
                from basemessages
                where $where
                order by msgDate desc limit $pageSize offset $offset";
        $data = yii::$app->db->createCommand($sql)->queryAll();
        $sqla = "select *
                 from basemessages
                 where $where ";
        $count = yii::$app->db->createCommand($sqla)->queryAll();
        return ['data' => $data, 'limit'=> intval($pageSize) , 'page'=> intval($page), 'statusCode'=>200, 'total'=> count($count)];
    }

//    public function getMessageData($post, $page, $pageSize)
//    {
//        $where = 1;
//        $offset = max(0, ($page-1))*$pageSize;
//        if (isset($post['s']) && !empty($post['s'])){
//            $s = $post['s'].' 00:00:00';
//            $where .= " and msgDate >= '{$s}' ";
//        }
//        if (isset($post['f']) && !empty($post['f'])){
//            $f = $post['f'].' 23:59:59';
//            $where .= " and msgDate <= '{$f}' ";
//        }
////        $sql = "select *
////                from basemessages
////                where $where
////                order by msgDate desc limit $pageSize offset $offset";
//        $sql = "select * from
//                ((select * from basemessages)
//                union all
//                (select ID AS msgId, 3 AS typeId, concat_ws(':','账号',AccountInfo,'订单号',DealNumber) as content, CheckFlag as msgStatus, CreateTime as msgDate from ExchangeGoldRecord)
//                )tt where $where
//                order by msgDate desc limit $pageSize offset $offset";
//        $data = yii::$app->db->createCommand($sql)->queryAll();
////        $sqla = "select *
////                 from basemessages
////                 where $where ";
//        $sqla = "select * from
//                ((select * from basemessages)
//                union all
//                (select ID AS msgId, 3 AS typeId, concat_ws(':','账号',AccountInfo,'订单号',DealNumber) as content, CheckFlag as msgStatus, CreateTime as msgDate from ExchangeGoldRecord)
//                )tt where $where";
//        $count = yii::$app->db->createCommand($sqla)->queryAll();
//        return ['data' => $data, 'limit'=> intval($pageSize) , 'page'=> intval($page), 'statusCode'=>200, 'total'=> count($count)];
//    }


    public function editData($post)
    {
        $where = "msgId in ({$post['ids']})";
        $sql = "update basemessages set msgStatus=1 where $where ";
        $result = yii::$app->db->createCommand($sql)->execute();
        if ($result > 0){
            return ['statusCode'=>200, 'message'=>'设置系统消息为已读'];
        }
        return ['statusCode'=>300, 'message'=>'设置系统消息失败'];
    }

    public function handleData($post)
    {
        $where = "msgId in ({$post['ids']})";
        //$sql = "update basemessages set msgStatus=1 where $where ";
        $sql = "delete from basemessages where $where";
        $result = yii::$app->db->createCommand($sql)->execute();
        if ($result > 0){
            return ['statusCode'=>200, 'message'=>'成功清理系统消息'];
        }
        return ['statusCode'=>300, 'message'=>'系统消息已清理'];
    }
}
